from PyQt4.QtCore import *
from PyQt4.QtGui import *

from ops import InputItem, CancelException

import properties
from value import Ui_Value
class Number(InputItem):
    name = "Number"
    def __init__(self,x,y):
        InputItem.__init__(self,x,y,[],["Out"],"Number")
        self.setToolTip("A single number")
        self.outputs[0].value = 0.00
        if not self.properties():
            raise CancelException
    def displayText(self):
        return str(self.outputs[0].value)
    def properties(self):
        d = properties.dialog(Ui_Value())
        d.ui.value.setValue(self.outputs[0].value)
        if properties.show(d):
            command = CommandEditValue(self,d.ui.value.value())
            if self.scene():
                self.scene().undoStack.push(command)
            else:
                command.redo()
            return True
        else:
            return False
        
    def process(self):
        """ nothing to do here, value is already set """
        pass

class CommandEditValue(QUndoCommand):
    def __init__(self,item,newValue):
        QUndoCommand.__init__(self,"Edit number")
        self.newValue = newValue
        self.item = item
        self.oldValue = item.outputs[0].value

    def redo(self):
        item = self.item
        item.outputs[0].value = self.newValue
        self.update()

    def undo(self):
        item = self.item
        item.outputs[0].value = self.oldValue
        self.update()

    def update(self):
        item = self.item
        item.setPlainText(item.displayText())
        item.locateOutputHandles()
        item.update(item.boundingRect())
        
